function login(app) {
  console.log(app)
  var that = this;
  wx.showLoading({ title: '正在登陆' });
  wx.login({
    success: function (res) {
      console.log("微信端登录成功");
      console.log(app)
      var code = res.code;
      console.log(res)
      wx.request({
        url: app.globalData.host + '/api/account/wxopenlogin',
        data: {
          code: code,
          storeid: app.globalData.storeId,
          storeAppId: app.globalData.storeAppId
        },
        success: function (res) {
          if (res.data.Code == 2002) {
            console.log("服务端没有发现对于OPENID的用户，去注册");
            that.registerUser(app);
            return;
          }
          wx.hideLoading();
          if (res.data.Code != 0) {
            console.log(res);
            console.log("服务端登录发生错误");
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
            return;
          }
          console.log("服务端登录成功");
          
          app.globalData.userInfo=res.data.Data;

        },
        fail:function(res){
  
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: res.errMsg,
            showCancel: false
          })
        }
      })
    }, fail: function (res) {
      wx.hideLoading();
       wx.showModal({
        title: '提示',
        content: res.errMsg,
        showCancel: false
      })
    }
  })
}
function registerUser(app){
  console.log("开始注册用户");
  var that = this;
  wx.login({
    success: function (res) {
      var code = res.code;
      wx.getUserInfo({
        success: function (res) {
          console.log("获取微信端用户信息成功");
          var iv = res.iv;
          var encryptedData = res.encryptedData;
          console.log(res);
          // 下面开始调用注册接口
          wx.request({
            url: app.globalData.host + '/api/account/WxOpenRegister',
            data: { code: code, encryptedData: encryptedData, iv: iv, storeid: app.globalData.storeId, storeAppId: app.globalData.storeAppId}, // 设置请求的 参数
            success: (res) => {
              wx.hideLoading();
              if (res.data.Code == 0) {
                console.log("注册用户成功，返回继续登录");
                that.login(app);
              } else {
                console.log("注册用户失败");
                console.log(res);
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }

            }
          })
        }
      })

    }
  })

}
module.exports = {
  login: login,
  registerUser: registerUser
}